

cd "\\micro.intra\projekt\P0844$\P0844_Data"
 
 

******************
*** Data Setup ***
****************** 
 
* Loop to append waves of the work environment survey 
clear
gen drop = .
save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo.dta", replace
 
foreach year in 1991 1993 1995 1997 1999 2001 2003 2005 2007 2009 2011 2013 2015 {
 
	use "Rickne_Lev_Arbmiljo_`year'.dta", clear
	gen year =`year' 
	append using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo.dta"
	save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo.dta", replace
	}
 
	* use this to drop observations in the loop for LISA data
	keep lopnr
	duplicates drop lopnr, force
	save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo_id.dta", replace

	use "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta", clear

* Link to LISA information, separated by 2007 because of the industry code.
	
cd "\\micro.intra\projekt\P0844$\P0844_Data"

clear
gen drop = .
save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta", replace

foreach year in 1995 1997 1999 2001 2003 2005 2007 2009 2011 2013 2015 {
 
	use "\\micro.intra\projekt\P0844$\P0844_Data\Rickne_Lev_Lisa_`year'.dta", clear
	ren peorgnr_lopnr orgnr
 	gen year = `year'	 
	
	* rename industry code
	if year<2003 {
		ren astsni92 ind_code
	}
	if year>2002 & year<2008 {
		ren astsni2002 ind_code
	}
	if year>2007 {
		ren astsni2007 ind_code
		* GÖR 2-DIGIT DO-FILE CONCORDANCE HERE!
		* dubbelkolla att ind_code kommer ut som string
	}
	if year>2004 {
		ren dispink04 dispink
	}
	
	joinby lopnr using "\\micro.intra\Projekt\P0844$\P0844_Data\Rickne_Lev_Bakgrund.dta"
	destring fodelsear, replace force

	gen age = year-fodelsear
	
	
		
	if year>1995 & year<2005 {
		preserve
		clear
		cd "\\micro.intra\projekt\P0844$\P0844_Data"
		gen drop =.
		append using "Rickne_Lev_Privat_`year'.dta"
		append using "Rickne2_Lev_arbetare_`year'.dta"
		append using "Rickne_Lev_Kommun_`year'.dta"
		append using "Rickne_Lev_Stat_`year'.dta"
		ren peorg orgnr 
	
		keep lopnr ssyk orgnr  manl
		save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp_sls.dta", replace
		restore
		joinby lopnr orgnr  using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp_sls.dta", unmatched(master)
		erase "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp_sls.dta"
	}
if year>2004 {
		preserve
		clear
		cd "\\micro.intra\projekt\P0844$\P0844_Data"
		gen drop =.
		append using "Rickne_Lev_Privat_`year'.dta"
		append using "Rickne2_Lev_arbetare_`year'.dta"
		append using "Rickne_Lev_Kommun_`year'.dta"
		append using "Rickne_Lev_Stat_`year'.dta"
		ren peorg orgnr 
	
		keep lopnr orgnr  manl
		save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp_sls.dta", replace
		restore
		joinby lopnr orgnr  using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp_sls.dta", unmatched(master)
		erase "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\temp_sls.dta"
	}
	* 97-99: No occupation code in LISA, use code from SLS
	* 01-03: Incomplete occupation code in LISA, use code from SLS if missing
	if year==1995 {
		gen ssyk=""
		gen manl=.
	}
	if year>2000 & year <2005 {
		replace ssyk = ssyk4 if ssyk4!="" & ssyk4!="****" 
	}
	if year>2004 {
		ren ssyk4 ssyk 
				
	}
	
	if year <2000 {
		gen ssykar=""
	}
	
	keep lopnr orgnr ssyk cfarnr_lopnr sun2000niva sun2000grp astkommun ind_code ///
		arblostyp dispink fodelsear kon year age fodelseland_eu28 ssykar manl 

	gen w=kon=="2"

	gen one=1
	gen ssyk3 = substr(ssyk,1,3)
	gen ind2 = substr(ind_code, 1,2)
	duplicates drop cfarnr_lopnr orgnr lopnr year, force
	foreach var in w{
	
	bysort cfarnr_lopnr orgnr: egen workpl_`var'=mean(`var') if cfarnr_lopnr!=. & orgnr!=.
		bysort ssyk3: egen ssyk3_`var'=mean(`var')	if ssyk3!="***" & ssyk3!=""

	}
		
		
	bysort cfarnr_lopnr orgnr: egen workpl_size= sum(one)

	bysort ssyk3: egen ssyk3_size=sum(one)	
	
	*limit data set to those in survey
		
	joinby lopnr using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo_id.dta"

	
	keep lopnr year orgnr cfarnr_lopnr sun2000niva sun2000grp astkommun ind_code ///
		 fodelsear dispink kon workpl_* ssyk3_*   manl ///
		 ssyk3 fodelseland_eu28 
 
	append using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta"
	save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta", replace
	tab year
	
	}          
//





use "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta", clear
*Basic data cleaning
	duplicates drop lopnr year, force
	tab workpl_w if kon=="2" & workpl_size==3
	gen woman=kon=="2"
	gen workpl_w_same= workpl_w
	replace workpl_w = ((workpl_size*workpl_w)-1)/(workpl_size-1) if woman==1
	replace workpl_w = ((workpl_size*workpl_w))/(workpl_size-1) if woman==0

	tab year
	save "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta", replace
	

*	Merge the two datasets

	use "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\arbmiljo.dta", clear
	joinby lopnr year using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\LISA_arbmiljo.dta", unmatched(both)
	* duplicates report lopnr year
	duplicates drop lopnr year, force
	
	tab year

	* kolla om det är vissa av survey:en som inte har någon registerinfo!
	* tab _merge if year>1996
	* bara 28 observationer är omatchade, 112,021 har match i LISA.
	*Drop the 28 observations with no match in regiser data. 
	keep if _merge==3

	*clean industry code variable
	destring ind_code, replace force
	gen sni07 = ind_code if year>2007

	do "\\micro.intra\Projekt\P0844$\P0844_Gem\useful plugin codes\sni2007 to sni2002.do" 	
	replace ind_code = sni07 if year>2007
	drop sni07
	gen ind2 =int(ind_code/1000)
	replace ind2=. if ind2==0
*	tab ind2 if ind2>80 & ind2<90
	

	*define and code variables used in analysis
	destring fodelsear, replace force
	gen age = year-fodelsear
	gen age_cat=.
	replace age_cat=1 if age<=35
	replace age_cat=2 if age>=36 & age<=50
	replace age_cat=3 if age>= 51 & age<=64
	replace age_cat=4 if age>= 65 & age!=.
	* drop if age<25 */ glass ceiling */
	
	* foreign-born

	gen europe = 1 if fodelseland_eu28=="EU28 utom Norden"
	replace europe = 1 if fodelseland_eu28=="Europa utom EU28 och Norden"
	replace europe = 1 if fodelseland_eu28=="Norden utom Sverige"
	replace europe = 0 if europe==.
	replace europe =. if fodelseland_eu28==""

	gen sweden = 1 if fodelseland_eu28=="Sverige"
	replace sweden = 0 if fodelseland_eu28!="Sverige"
	replace sweden =. if fodelseland_eu28==""
	
	gen non_europe = 1 if sweden!=1 & europe!=1
	replace non_europe = 0 if sweden==1 | europe==1
	replace non_europe =. if fodelseland_eu28==""
	
	* Education level
	replace sun2000niva="" if sun2000niva=="*" | sun2000niva=="-"
	destring sun2000niva, replace force
	replace  sun2000niva=. if sun2000niva>=999

	gen educ_year=.
	replace educ_year= 7.5 if sun2000niva>=100 & sun2000niva<200
	replace educ_year= 9.4 if sun2000niva>=200 & sun2000niva<300
	replace educ_year= 11.2 if sun2000niva>=300 & sun2000niva<330
	replace educ_year= 12.4 if sun2000niva>=330 & sun2000niva<400
	replace educ_year= 14.2 if sun2000niva>=410 & sun2000niva<530
	replace educ_year= 17 if sun2000niva>=530 & sun2000niva<600
	replace educ_year= 20.4 if sun2000niva>=600 & sun2000niva<=999	
	gen ter_ed=educ_year>= 14 & educ_year!=.
	
	gen  workpl_m = 1-workpl_w
	gen  workpl_m_same = 1-workpl_w_same
	gen exec=arbetsle==1 if arbetsle!=.
	*Define harassment variables
	gen hara_coll = 1 if chefkam6<7 
	replace hara_coll = 0 if chefkam6==7
	gen sex_trak= chekasex<7 if chekasex!=. & chekasex<8
	gen hara_comb= sex_trak==1|hara_coll==1 if sex_trak!=. & hara_coll!=.
	gen high_freq =  chekasex<6 | chefkam<6 if  chekasex!=. & chefkam!=. 
	gen vhigh_freq =  chekasex<5 | chefkam<5 if  chekasex!=. & chefkam!=. 

*Define variable for other work conditions and feelings about work
    gen bully =  mobbing<7 if mobbing mobbing<=7 


	gen conf_coll = kamrbrak<7 if  kamrbrak<=7 	
	gen conf_boss= chefbrak<7 1 if chefbrak<=7 
	gen conflict= conf_boss==1|conf_coll==1 if conf_boss!=. & conf_coll!=.
	gen unhappy = olust<5 if olust<=5 
	replace unhappy = 0 if olust==5
	gen chef_sup=5-cheferst
	gen coll_sup=5-kamratst
    gen chef_app=6-chefupps
	gen coll_app=6-andraupp
	gen coll_napp=6-coll_app 
	gen chef_napp=6-chef_app 
	gen coll_nsup=5-coll_sup 
	gen chef_nsup=5-chef_sup 
 
	gen leave_cons=bliforet!=3 if bliforet!=.
	gen olust_rev=6-olust
	gen missnojd_rev=6-missnojd
	gen olust_d=olust_rev>1 if olust_rev!=.
	gen missnojd_d =missnojd_rev>3 if missnojd_rev!=.
	gen no_flex= flextid==3 if flextid!=.
	gen overtime= 6-overtide
	gen fem_bos= chefkvi==2 if chefkvi<3

	*Add data population numbers in categories used to construct weights
	joinby lopnr year using "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\weights_arb.dta", unmatched(master) _merge(merge_weights)

	*Define weights
	gen main_sample=hara_comb!=. & workpl_size>4 
	bysort year : egen samp_tot=sum(main_sample)
 
	foreach group in gen_edu2_alder3_pinc3_br gen_edu2_alder3_pinc3  gen_edu2_alder5  gen_edu2_alder3_ssyk2   gen_ssyk2 gen_edu_alder5_ssyk3 gen_edu_alder5  {
		bysort year `group': egen `group'_samptot=sum(main_sample)
		gen `group'_w=(`group'_tot/syss_tot)/(`group'_samptot/samp_tot)
	}
 
cd "\\micro.intra\Projekt\P0844$\P0844_Gem\Arbetsmiljö\"
*Add additional data sets
	compress
	joinby lopnr year using child_panel_arb.dta, unmatched(master) _merge(_mergechild3)
	drop _merge
	joinby  cfarnr_lopnr  using firm_fe_wage, unmatched(master)
	drop _merge
	joinby  cfarnr_lopnr  using firm_fe_inc, unmatched(master)
	drop _merge
	joinby lopnr year using new_switch, unmatched(master)
	drop _merge
	joinby lopnr  using ind_fe_wage, unmatched(master)
	drop _merge
	joinby ssyk3  using occ_wagerank, unmatched(master)
	
	*define additional variables used as controls or for descriptive statistics 

	destring fams, replace force
	replace barn=0 if fams>300 & fams<400
	replace new_child=0  if fams>300 & fams<400
	replace first_child=0  if fams>300 & fams<40
	replace formaga=. if formaga>10
	gen man =woman==0

	gen workpl_m_int= int(workpl_m*100) if main_samp==1
	bysort workpl_m_int: egen temp=mean(hara_comb) if woman==1 & main_samp==1
	bysort workpl_m_int: egen hara_risk_w=mean (temp) if  main_samp==1
	drop temp
	bysort workpl_m_int: egen temp=mean(hara_comb) if woman==0 & main_samp==1
	bysort workpl_m_int: egen hara_risk_m=mean (temp) if  main_samp==1
	drop temp

	gen famtyp=1 if  fams>=100 & fams<=200 & barn>=1 & barn!=.
	replace famtyp=2 if  fams>=100 & fams<=200  & barn==0
	replace famtyp=3 if ((fams>=200 & fams<300) | fams==400)  & barn>=1 & barn!=.
	replace famtyp=4 if   (fams>=300 & fams<400) |  (barn==0 & fams==400)
	replace famtyp=5 if   famtyp==.
	gen edu3=1 if   educ_year>= 7 & educ_year<=10
	replace edu3=2 if   educ_year>=11 & educ_year<=13
	replace edu3=3 if   educ_year>= 14 & educ_year<=21

	gen fsize_cat=1 if   workpl_size>=5 &  workpl_size<=10
	replace fsize_cat=2 if    workpl_size>=11 &  workpl_size<=25
	replace fsize_cat=3 if   workpl_size>=26 &  workpl_size<=100
	replace fsize_cat=4 if   workpl_size>=101 &  workpl_size<=500
	replace fsize_cat=4 if   workpl_size>=500 &  workpl_size!=.

	gen mshare_cat= 1 if  workpl_m>=0 &  workpl_m<=.2
	replace  mshare_cat = 2   if   workpl_m>.2 &  workpl_m<=.4
	replace  mshare_cat = 3  if  workpl_m>.4 &  workpl_m<=.6
	replace  mshare_cat = 4 if     workpl_m>.6&  workpl_m<=.8
	replace  mshare_cat = 5 if  workpl_m>.8&  workpl_m<=1

	xtile fpay_cat=firm_fe_5 if main==1, nq(5)
	gen high_prod=formaga==10 if formaga!=.
	egen med_indfe= median(ind_fe_5) if main_samp==1
	gen high_ind_fe = ind_fe_5>med_indfe if ind_fe_5!=. & main_samp==1
	
	gen hw= firm_fe_5 >=0 if firm_fe_5!=.
	gen malemaj_wp= workpl_m>.5 if workpl_m!=.

	gen ssyk1=substr(ssyk3, 1,1)
	destring ssyk1, force replace

	xtile ind_feq= ind_fe if main==1

** Generate PC Score for bad work conditions
	global xlist bully  coll_napp chef_napp  coll_nsup chef_nsup   conf_coll  conf_boss
	pca $xlist if main==1 & hara_comb!=.
	screeplot, yline(1)
	estat loadings 
	estat kmo
	predict pc1 pc2, score
	replace pc1 = 0-pc1



	
	
	
	
	
